package com.powerhua.core.base.dao;

import com.powerhua.core.base.BaseModelCacheManager;
import com.powerhua.core.base.bean.IField;
import com.powerhua.core.base.bean.IModel;
import com.powerhua.core.db.UUIDGenerator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Brian.Xie
 */
public class SaveDao extends CommonDao {

    private static final Log LOG = LogFactory.getLog(SaveDao.class);

    /**
     *
     * @param map
     * @param baseModelId
     * @return
     */
    public String save(Map map, String baseModelId) {
        // 从缓存中取出对应模型
        IModel model = BaseModelCacheManager.getInstance().getModel(baseModelId);
        if (model == null) {
            LOG.error("No model found");
            return "";
        }

        // 取出模型主键
        IField pkField = model.getPk();
        if (pkField == null) {
            LOG.error("No primary key found");
            return "";
        }

        // 从模型中取出字段
        Map<String, IField> fields = model.getFields();
        if (fields == null || fields.size() == 0) {
            LOG.error("No table field found");
            return "";
        }

        // 定义SQL语句存放
        StringBuffer sql = new StringBuffer();
        sql.append("insert into " + model.getModelTable() + " ");
        StringBuffer sqlColumn = new StringBuffer(pkField.getName().toLowerCase());
        StringBuffer sqlValue = new StringBuffer("?");

        // 定义值和类型存放
        List<String> values = new ArrayList<String>();
        List<String> types = new ArrayList<String>();

        // 生成SQL语句
        Iterator<Entry<String, IField>> itr = fields.entrySet().iterator();
        while (itr.hasNext()) {
            Entry<String, IField> entry = itr.next();
            IField field = entry.getValue();
            String columnName = field.getName().toLowerCase();
            String columnType = field.getType().toLowerCase();
            if (map.get(columnName) != null) {
                sqlColumn.append(", " + columnName);
                sqlValue.append(",?");
                values.add(((String[]) map.get(columnName))[0]);
                types.add(columnType);
            }
        }
        sql.append("(");
        sql.append(sqlColumn);
        sql.append(")");
        sql.append(" values ");
        sql.append("(");
        sql.append(sqlValue);
        sql.append(")");

        // jdbcTempldate action
        Object[] objs = new Object[values.size() + 1];
        String uuid = UUIDGenerator.getUUID();
        objs[0] = uuid;
        for (int i = 0; i < values.size(); i++) {
            String type = types.get(i).toLowerCase();
            Object obj = convertType(type, values.get(i));
            if (obj == null) {
                LOG.error("Convert type value error");
                return "";
            }
            objs[i + 1] = obj;
        }
        int dbResult = jdbcTemplate.update(sql.toString(), objs);

        return "";
    }
}
